//学生课程留言


import React, { useState } from 'react';
import { Layout, Select, Input, Button, List, Modal, Form, message } from 'antd';
import { sentCourseMessageAPI } from '@/api/student';
import { getuserName } from '@/utils';

const { Header, Content } = Layout;
const { Option } = Select;
const { TextArea } = Input;

function getCurrentFormattedTime() {
  // 获取当前日期时间
  const now = new Date();

  // 获取年、月、日、小时（注意JavaScript中月份是从0开始计数的，所以需要+1）、分钟
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份补零
  const day = String(now.getDate()).padStart(2, '0'); // 日期补零
  // const hours = String(now.getHours()).padStart(2, '0'); // 小时补零
  // const minutes = String(now.getMinutes()).padStart(2, '0'); // 分钟补零

  // 格式化时间字符串
  const formattedTime = `${year}-${month}-${day}`;

  return formattedTime;
}

const StudentFeedback = () => {
  const [selectedCourse, setSelectedCourse] = useState(null);
  const [messages, setMessages] = useState([]);
  const [inputValue, setInputValue] = useState('');
  const [coachInput,setCoachInput] = useState('')

  const handleCourseChange = (value) => {
    setSelectedCourse(value);
  };

  //发送留言
  const handleMessageSend =async () => {
    if (inputValue.trim() !== '') {
      setMessages([...messages, { course: selectedCourse, message: inputValue }]);
      setInputValue('');
      const studentName =getuserName()
      const currentTime =getCurrentFormattedTime() 
      await sentCourseMessageAPI({
        evaluateTime:currentTime,
        businessName:selectedCourse,
        studentName:studentName,
        coachName:coachInput,
        studentEvaluateContent:inputValue,
        coachIsReplied:0
      })
      message.success('信息发送成功!');
    } else {
      message.error('请输入正确信息!');
    }
  };

  return (
    <Layout>
      <Header style={{ color: 'white', fontSize: '0.6rem' }}>
      <h1>学生课程留言</h1>
     </Header>
      <Content style={{ padding: '0 50px' }}>
        <div style={{ background: '#fff', padding: 24, minHeight: 280 }}>
          <Select
            style={{ width: 200, marginBottom: 20 }}
            placeholder="请选择科目"
            onChange={handleCourseChange}
          >
            <Option value="科目一">科目一</Option>
            <Option value="科目二">科目二</Option>
            <Option value="科目三">科目三</Option>
            <Option value="科目四">科目四</Option>
          </Select>
          <div>
          <Input value={coachInput} 
          style={{ width: 200, marginBottom: 20 }} 
          placeholder='请输入教练名称'
          onChange={(e)=>setCoachInput(e.target.value)}
          />
          </div>
          
          <List
            bordered
            dataSource={messages}
            renderItem={(item, index) => (
              <List.Item>
                <List.Item.Meta
                  title={<div>{`留言 ${index + 1} 科目:${selectedCourse} 教练:${coachInput}`}</div>}
                  description={item.message}
                />
              </List.Item>
            )}
          />
          <div style={{ marginTop: 20 }}>
            <TextArea
              rows={4}
              value={inputValue}
              onChange={(e) => setInputValue(e.target.value)}
              placeholder="请输入您想留言的内容......"
            />
            <Button
              type="primary"
              style={{ marginTop: 10 }}
              onClick={handleMessageSend}
              disabled={!selectedCourse}
            >
              发送
            </Button>
          </div>
        </div>
      </Content>
    </Layout>
  );
};

export default StudentFeedback;